package LC;

/**
 * https://leetcode.com/problems/length-of-last-word/description/
 * Given a string s consists of upper/lower-case alphabets and empty space characters ' ',
 * return the length of last word in the string.
 * If the last word does not exist, return 0.
 * Note: A word is defined as a character sequence consists of non-space characters only.
 * For example,
 * Given s = "Hello World",
 * return 5.
 */
public class LC_058_LengthofLastWord_String_Search {
    public static void main(String[] args) {
        String s = "hello world ";
        int n = Solution.lengthOfLastWord(s);
        System.out.println(n);
    }

    static class Solution {
        static int lengthOfLastWord(String s) {
            int len = s.length(),
                    i = len - 1,
                    count = 0;
            while (i >= 0 && s.charAt(i) == ' ')
                i--;
            for (; i >= 0; i--) {
                char ch = s.charAt(i);
                if (ch != ' ')
                    count++;
                else
                    break;
            }
            return count;
        }
    }

}
